package com.codedancer.campus.client.api.systemMessage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.systemMessage.ro.SystemMessagePageSearchRo;
import com.codedancer.campus.client.service.ISystemMessageService;
import com.codedancer.campus.client.service.bo.systemMessage.SystemMessagePageSearchBo;
import com.codedancer.campus.client.service.vo.systemMessage.SystemMessageVo;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/system-message")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "系统消息Api", description = "系统消息Api", tags = "系统消息Api")
public class SystemMessageApi extends BaseApi {

    @Autowired
    private ISystemMessageService systemMessageService;

    @GetMapping("/page")
    @ApiOperation("分页查询系统消息")
    public ApiResponse<IPage<SystemMessageVo>> page(
        @ApiParam(value = "系统消息分页查询Ro", required = false)
            SystemMessagePageSearchRo systemMessagePageSearchRo) {
        SystemMessagePageSearchBo systemMessagePageSearchBo = BeanUtil
            .copyProperties(systemMessagePageSearchRo, SystemMessagePageSearchBo.class);
        systemMessagePageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(systemMessageService.page(systemMessagePageSearchBo));
    }

    @PostMapping("/single-read/{uuid}")
    @ApiOperation("单个消息已读")
    public ApiResponse<Void> singleRead(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "系统消息uuid", required = true) String uuid) {
        systemMessageService.singleRead(uuid);
        return success();
    }

    @PostMapping("/all-read")
    @ApiOperation("消息全部已读")
    public ApiResponse<Void> allRead() {
        systemMessageService.allRead(currentLoginUuid());
        return success();
    }

    @GetMapping("/is-exits-no-read")
    @ApiOperation("是否存在未读系统消息")
    public ApiResponse<Boolean> isExistsNoRead() {
        return success(systemMessageService.isExistsNoRead(currentLoginUuid()));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除系统消息")
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "系统消息uuid", required = true) String uuid) {
        systemMessageService.delete(uuid);
        return success();
    }
}
